-- UIGuideMask
-- Created by zouyb
-- 通用引导蒙版窗口

UIGuideMask = class("UIGuideMask", function()
    return cc.Layer:create();
end);

function UIGuideMask.create(targetForm, path, offsetX, offsetY, needInvert)
    return UIGuideMask.new(targetForm, path, offsetX, offsetY, needInvert);
end

-- 构造函数
function UIGuideMask:ctor(targetForm, path, offsetX, offsetY, needInvert)
    self:setName("UIGuideMask");
    local node = cc.CSLoader:createNode("layout/guide/GuideMask.csb");
    self:addChild(node);
    self.node = node;
    self.path = path;
    self.offsetX = offsetX or 0;
    self.offsetY = offsetY or 0;
    self.needInvert = needInvert;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    self:resize();

    -- 获得目标节点
    local targetNode = findChildByName(targetForm.node, path);

    -- 其他部分变暗
    -- turnFormDark(targetForm, true, { path });

    if targetNode then
        self:init(targetNode);
    end

    -- 点击回调处理
    EventMgr.removeAll("UIGuideMask");
    EventMgr.register("UIGuideMask", event.TARGET_CLICK, function(param)
        if param.object == targetNode then
            UIMgr.getCurrentScene():removeFormByName("UIGuideMask");
        end
    end);

    self:registerScriptHandler(function(event)
        if event == "exit" then
            -- turnFormDark(targetForm, false, { });
            EventMgr.removeAll("UIGuideMask");
        end
    end);

end

-- 适配
function UIGuideMask:resize()
    local node = self.node;

    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local panel = findChildByName(self.node, "panel");
    panel:setScaleX(w / DESIGN_WIDTH);
    panel:setScaleY(h / DESIGN_HEIGHT);
    panel:setPosition(w / 2, h / 2);
end

-- 初始化
function UIGuideMask:init(targetNode)
    local worldPos = targetNode:convertToWorldSpace(cc.p(0, 0));

    local panel = findChildByName(self.node, "panel");
    panel:setTouchEnabled(true);
    local nodePos = panel:convertToNodeSpace(worldPos);

    local rectNode = findChildByName(self.node, "panel/rect");
--    if DEBUG_MODE == 1 then
--        -- 测试模式时，增加点击范围区域显示
--        rectNode:setBackGroundColorType(ccui.LayoutBackGroundColorType.solid);
--        rectNode:setBackGroundColor(cc.c3b(0, 128, 0));
--    end

    rectNode:setPosition(nodePos);
    rectNode:setScaleX(targetNode:getScaleX());
    rectNode:setScaleY(targetNode:getScaleY());
    rectNode:setContentSize(targetNode:getContentSize().width, targetNode:getContentSize().height);

    local hand = rectNode:getChildByName("hand");
    hand:setVisible(true);
    hand:setPosition(rectNode:getContentSize().width / 2, rectNode:getContentSize().height / 2);

    if self.needInvert == true then
        playClickEffectInvert(hand, self.offsetX, self.offsetY);
    else
        playClickEffect(hand, self.offsetX, self.offsetY);
    end

    local panel = findChildByName(self.node, "panel");
    local function onCloseClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            local rect = rectNode:getBoundingBox();

            local locationInNode = panel:convertToNodeSpace(sender:getTouchBeganPosition());
            if cc.rectContainsPoint(rect, locationInNode) then
                -- 在目标范围区域， 设置消息穿透
                panel:setSwallowTouches(false);
                return false;
            end

            -- 不在目标范围区域， 吃掉消息
            panel:setSwallowTouches(true);

            -- 播放涟漪效果
            playEffect(panel, 1175, locationInNode.x, locationInNode.y);

            return true;
        end
    end
    panel:addTouchEventListener(onCloseClick);
end




